﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi.IntegrityCheck
{
    [CPPEI.WebApi.AuthGetAction("IntegrityCheck", "GetInstance", "1.0")]
    public class GetInstanceAction : BaseAction<GetInstanceParams, GetInstanceResult>
    {
        protected override ActionResult<GetInstanceResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetInstanceParams opParams)
        {
            SingleResult<CPPEI.ETL.Domain.Data.IntegrityCheckInstanceDetail> serResult = new SER.QueryService().GetIntegrityCheckInstance(opParams.Id);

            if (serResult.IsSuccess)
            {
                GetInstanceResult result = new GetInstanceResult();
                result.Id = serResult.Data.Entity.Id;
                result.Name = serResult.Data.IntegrityCheckName;
                result.Description = serResult.Data.IntegrityCheckDescription;
                for (int index = 0; index < serResult.Data.ParameterList.Count; index++)
                {
                    result.ParameterList.Add(new GetInstanceResult.Parameter() { Name = serResult.Data.ParameterList[index].Entity.Name, 
                        Caption = serResult.Data.ParameterList[index].Caption,
                        Type = serResult.Data.ParameterList[index].Type,
                        Value = serResult.Data.ParameterList[index].Entity.Value });
                }

                return new ActionResult<GetInstanceResult>(result);
            }
            else
            {
                return new ActionResult<GetInstanceResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetInstanceParams
    {
        /// <summary>
        /// 实例id
        /// </summary>
        public Guid Id { get; set; }
    }

    public class GetInstanceResult
    {
        /// <summary>
        /// 实例ID
        /// </summary>
        public Guid Id { get; set; }

        /// <summary>
        /// 类型名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 类型描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 参数列表
        /// </summary>
        public List<Parameter> ParameterList = new List<Parameter>();
        public class Parameter
        {
            /// <summary>
            /// 参数名称
            /// </summary>
            public string Name { get; set; }
            /// <summary>
            /// 参数描述
            /// </summary>
            public string Caption { get; set; }
            /// <summary>
            /// 类型
            /// </summary>
            public string Type { get; set; }
            /// <summary>
            /// 参数的值
            /// </summary>
            public string Value { get; set; }
        }
    }
}
